package com.xhwl.centiotdata.suppliersystem.guangtuo.alarmhost.websocket.handle.event;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.suppliersystem.guangtuo.alarmhost.websocket.handle.AlarmHostNotifyHandle;
import com.xhwl.centiotdata.suppliersystem.guangtuo.alarmhost.websocket.handle.AlarmHostNotifyHandleFactory;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author zhouyongzhou
 * @date 2023/04/28  17:39
 * 报警主机状态通知处理
 */
@Slf4j
@Service
public class AlarmHostStatusNotifyHandle extends AlarmHostNotifyHandle {
    private static final String TOPIC = "/ws/alarmhost/v2/notification/status";

    @Resource
    private IDeviceCentService iDeviceCentService;

    @Override
    public void afterPropertiesSet() throws Exception {
        AlarmHostNotifyHandleFactory.register(TOPIC, this);
    }

    @Override
    public void handler(String status, String data) {
        log.info("websocket-报警主机状态通知处理  TOPIC={},status={},data={}", TOPIC, status, data);
        JSONObject jsonObject = JSONObject.parseObject(data);
        String statusType = String.valueOf(jsonObject.get("statusType"));
        String statusValue = String.valueOf(jsonObject.get("statusValue"));
        String objID = String.valueOf(jsonObject.get("objID"));
        DevicePageQuery deviceQuery = new DevicePageQuery();
        deviceQuery.setDeviceTypeId(CategoryEnum.ALARM_HOST_SYSTEM.getId());
        List<DeviceDTO> deviceDTOList =iDeviceCentService.listDevice(null, deviceQuery);
        if (CollectionUtils.isEmpty(deviceDTOList)) {
            log.info("无设备类别{}...",CategoryEnum.ALARM_HOST_SYSTEM.getId());
        }
        List<DeviceDTO> modifyDeviceDTOList = new ArrayList();
        for (DeviceDTO deviceDTO : deviceDTOList) {
            Map<String, Object> alarmHostMap = new HashMap<>(2);
            JSONObject attributeJson = deviceDTO.getAttributeJson();
            if (null != attributeJson) {
                Object attributeData = attributeJson.get("data");
                if (null != attributeData) {
                    List<Object> alarmHostList = JSON.parseArray(String.valueOf(attributeData));
                    if (!CollectionUtils.isEmpty(alarmHostList)) {
                        for (int i = 0; i < alarmHostList.size(); i++) {
                            JSONObject alarmHostObject = JSON.parseObject(String.valueOf(alarmHostList.get(i)));
                            if (objID.equals(alarmHostObject.getString("objID"))) {
                                if (!statusValue.equals(alarmHostObject.getString(statusType))) {
                                    alarmHostObject.put(statusType, statusValue);
                                    alarmHostList.set(i, alarmHostObject);
                                    alarmHostMap.put("data", alarmHostList);
                                    deviceDTO.setAttributeJson(JSON.parseObject(JSON.toJSONString(alarmHostMap)));
                                    modifyDeviceDTOList.add(deviceDTO);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (!CollectionUtils.isEmpty(modifyDeviceDTOList)) {
                ResultJson resultJson = iDeviceCentService.batchUpdate(null, modifyDeviceDTOList);
                log.info("报警系统-websocket状态通知:{}", JSON.toJSONString(resultJson));
            } else {
                log.info("当期报警主机状态通知处理数据重复推送!");
            }
        }
    }
}